package shuashuami.hb.com.fragment.agent;

import android.view.View;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import shuashuami.hb.com.adapter.AClickManagerAdapter;
import shuashuami.hb.com.adapter.AClickManagerAdapter2;
import shuashuami.hb.com.entity.agent.ClickerBean;
import shuashuami.hb.com.entity.agent.PagerBean;
import shuashuami.hb.com.fragment.AbFragment;
import shuashuami.hb.com.hbclient.R;
import shuashuami.hb.com.http.HttpAgentMethods;
import shuashuami.hb.com.http.HttpOnNextListener;
import shuashuami.hb.com.http.LoginHttp;
import shuashuami.hb.com.util.LoadType;
import shuashuami.hb.com.view.pull.PullToRefreshLayout;

/**
 * <b>类   名：</b>${TYPE_NAME} <br/>
 * <b>类描述：</b> <br/>
 * <b>创建人：</b>zhangqi<br/>
 * <b>创建时间：</b>2017/6/23<br/>
 * <b>修改人：</b>zhangqi<br/>
 * <b>修改时间：</b>2017/6/23<br/>
 * <b>修改备注：</b><br/>
 *
 * @version 1.0.0<br/>
 */
public class ClickMangeFragm extends AbFragment {
    public enum ClickType {

        ALL_CLICK, CHECK_PEND

    }

    private ClickType clickType;

    private ListView listView;

    private BaseAdapter adapter;



    private int currentPage=1;

    private int currentLevel=0;//全部刷手  一级刷手   二级刷手

    private int currentType=1;// 1已认真 2 代表待审核

    private int page=0;

    private PagerBean pager;

    private PullToRefreshLayout mptrl;

    private List<ClickerBean> mResource;

    private List<ClickerBean> mDisplayResource;

    public int operateType = LoadType.NO_OPERATE;

    public ClickMangeFragm(){

    }

    @Override
    public void initView(View v) {
        listView= (ListView) findViewById(R.id.lv_listview);
        int type=getArguments().getInt("type");
        mResource=new ArrayList<>();
        mDisplayResource=new ArrayList<>();
        if(type==1){

            clickType=ClickType.ALL_CLICK;
            adapter = new AClickManagerAdapter(getActivity(), mDisplayResource);
        }
        else if(type==2){
            clickType=ClickType.CHECK_PEND;
            adapter=new AClickManagerAdapter2(getActivity(),mDisplayResource);
        }

        currentType=type;

        listView.setAdapter(adapter);


        mptrl= (PullToRefreshLayout) findViewById(R.id.refresh_view);

    }

    @Override
    public void initData() {


        getDataFromSever();

    }

    @Override
    public void initListener() {
        mptrl.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {


            @Override
            public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                //
                operateType= LoadType.DO_REFERSH;
                currentPage=1;
                getDataFromSever();

            }

            @Override
            public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
                if(pager!=null && pager.getMaxpage()==pager.getCurrentpage()){
                    mptrl.loadmoreFinish(PullToRefreshLayout.NODATA);
                    return;
                }

                operateType= LoadType.LOAD_MORE;
                currentPage++;
                getDataFromSever();
            }
        });

    }

    @Override
    public int bindView() {
        return R.layout.frag_clickmanage;
    }


    public void getDataFromSever(){

        HttpOnNextListener<String> httpOnNextListener=new HttpOnNextListener<String>() {
            @Override
            public void onNext(final String s) {

                getAbActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        resolveByJson(s);
                    }
                });
            }
        };


        HttpAgentMethods.getMyClicker(httpOnNextListener,currentType,currentLevel,currentPage);
    }


    public void resolveByJson(String str){
        try{
            JSONObject object=new JSONObject(str);
            if (operateType == LoadType.DO_REFERSH || operateType==LoadType.NO_OPERATE) {
                mResource.clear();
            }

            int status=object.getInt("status");
            if (status == 203) {
                LoginHttp loginHttp = new LoginHttp(getActivity());
                loginHttp.login();
                return;
            }
            if(status==200) {
                JSONObject content=object.getJSONObject("content");

                PagerBean page = new PagerBean();
                page.setCount(content.getInt("count"));
                page.setMaxpage(content.getInt("maxpage"));
                page.setCurrentpage(content.getInt("currentpage"));
                JSONArray list=content.getJSONArray("list");
                List<ClickerBean> childlist=new ArrayList<>();
                for(int i=0;i<list.length();i++){
                    JSONObject item=list.getJSONObject(i);
                    ClickerBean bean=new ClickerBean();
                    bean.resolveByJson(item);
                    childlist.add(bean);
                }
                page.setList(childlist);

                // 开始解析

                pager = page;
//                // 如果是刷新

                mResource.addAll(page.getList());
            }
            else{
                String message=object.getString("message");
                Toast.makeText(getActivity(),message,Toast.LENGTH_LONG).show();
            }

            disPlayData("");

            // 如果是刷新
            if (operateType == LoadType.DO_REFERSH) {
                mptrl.refreshFinish(PullToRefreshLayout.SUCCEED);
            }
            if (operateType == LoadType.LOAD_MORE) {
                mptrl.loadmoreFinish(PullToRefreshLayout.SUCCEED);
            }

            operateType = LoadType.NO_OPERATE;


        }catch (Exception e){
            e.printStackTrace();
            Toast.makeText(getActivity(),"json解析失败",Toast.LENGTH_LONG).show();
        }

    }

    public void disPlayData(String id){
        mDisplayResource.clear();
        for(int i=0;i<mResource.size();i++){
            if(id.equals("") || mResource.get(i).getId().equals(id)){
                mDisplayResource.add(mResource.get(i));
            }
        }

        adapter.notifyDataSetChanged();
    }

    // 设置当前的等级
    public void setCurrentLevel(int level){
        currentPage=1;
        currentLevel=level;
        getDataFromSever();

    }
}
